package cn.edu.neosoft.sunbo509.finefood.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neosoft.sunbo509.finefood.Activity.ShopdetailActivity;
import cn.edu.neosoft.sunbo509.finefood.Bean.ShoplistBean;
import cn.edu.neosoft.sunbo509.finefood.R;

/**
 * Created by 小博 on 2017/10/30.
 */

public class ShoplistAdapter extends RecyclerView.Adapter {
     private List<ShoplistBean> list;
     private Context context;
     private LayoutInflater layoutInflater;
     private String userid;

    public ShoplistAdapter(Context context) {
        this.context=context;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setList(List<ShoplistBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }
    public void setUserid(String id){
        this.userid=id;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shoplist, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ShoplistBean shoplistBean=list.get(position);
        ViewHolder viewHolder= (ViewHolder) holder;
        if(shoplistBean==null) {
            return;
        }
        Picasso.with(context)
                .load(shoplistBean.getPic())
                .placeholder(null)
                .into(viewHolder.iv_pic);
        viewHolder.tv_shopname.setText(shoplistBean.getShopname());
        viewHolder.tv_address.setText(shoplistBean.getAddress());
        viewHolder.rb_rank.setRating(shoplistBean.getLevel());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(context,ShopdetailActivity.class);
                intent.putExtra("shopid",list.get(position).getShop_id());
                intent.putExtra("shopname",list.get(position).getShopname());
                intent.putExtra("userid",userid);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
       if(list==null)
           return 0;
        else
            return list.size();
    }

    public class ViewHolder extends  RecyclerView.ViewHolder{
          private ImageView iv_pic;
          private TextView tv_shopname,tv_address;
          private RatingBar rb_rank;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_pic=(ImageView)itemView.findViewById(R.id.iv_pic);
            tv_address=(TextView)itemView.findViewById(R.id.tv_address);
            tv_shopname=(TextView)itemView.findViewById(R.id.tv_shopname);
            rb_rank=(RatingBar)itemView.findViewById(R.id.rb_rank);
        }
    }

}
